<template>
    <div class="container">
        <!-- 文章详情 -->
        <div class="content">
            <van-image class="img" fit="cover" :src="info.picUrl" />
            <div class="body">
                <p class="title">{{ info.title }}</p>
                <div class="info">
                    <p class="desc">{{ info.desc }}</p>
                    <p class="created_time">{{ info.createdAt }}</p>
                </div>
                <div class="detail">
                    {{ info.detail }}
                </div>
            </div>
        </div>
        <!-- 评论 -->
        <div class="comment" v-if="info.comments">
            <p>评论:</p>
            <div class="content_item" v-for="it in info.comments">
                <div class="user_pic">
                    <img :src="it.user.avatar" :alt="it.user.name" />
                </div>
                <div class="item_left">
                    <div class="user_name">
                        {{ it.user.name }}&emsp;<span class="createdTime">{{
                            it.created_at
                        }}</span>
                    </div>
                    <div class="user_content">{{ it.content }}</div>
                </div>
            </div>
        </div>
        <!-- 底部操作栏 -->
        <div class="comment">
            <div class="action_box">
                <div class="add_comment" @click="showBottom = true">
                    <van-icon name="edit" size="20px" />&nbsp;评论
                </div>
                <div class="action">
                    <div class="Thumbs_action" @click="praise">
                        <van-icon :color="isPraise ? '#3498db' : '#767676'" :name="isPraise ? 'good-job' : 'good-job-o'"
                            :badge="info.praise || ''" :badge-props="{ offset: ['10px', '20pxpx'] }" />
                    </div>
                    <div class="star_action" @click="collect">
                        <van-icon :color="isCollect ? '#f39c12' : '#767676'" :name="isCollect ? 'star' : 'star-o'"
                            :badge="info.collect || ''" />
                    </div>
                </div>
            </div>
        </div>
        <!-- 底部弹出层 -->
        <van-popup class="comment_box" v-model:show="showBottom" position="bottom" :style="{ height: '30%' }">
            <div class="comment_header">
                <div>评论</div>
                <div class="left" @click="commentAdd">发布</div>
            </div>
            <van-field label-align="top" v-model="comment" rows="6" type="textarea" maxlength="200" placeholder="请输入评论"
                show-word-limit />
        </van-popup>
    </div>
</template>

<script setup>
import { onMounted, ref } from "vue";
import { commentDetail, addComment } from '@/api/comment';
import { useRoute } from "vue-router";
import { collecdAdd, collecdDelete } from "@/api/collect";
import { praiseAdd, praiseDelete } from "@/api/praise";
import { showSuccessToast } from "vant";

// 控制底部 弹出层展示
let showBottom = ref(false);

// 收集输入的评论
let comment = ref("");
//
let route = useRoute();
// 文章详情
let info = ref({});

// 是否收藏
let isCollect = ref(false);
// 是否点赞
let isPraise = ref(false);

// 点赞
let praise = async () => {
    if (isPraise.value) {
        // 取消点赞逻辑
        await praiseDelete({ type: 2, object_id: route.query.articleId })
        showSuccessToast("取消点赞成功");
    } else {
        isPraise.value = true;
        await praiseAdd({ type: 2, object_id: route.query.articleId });
        showSuccessToast("点赞成功");
    }
    // 重新调用  文章详情
    getDetali();
};
// 收藏
let collect = async () => {
    if (isCollect.value) {
        // 取消收藏逻辑
        await collecdDelete({ type: 2, object_id: route.query.articleId })
        showSuccessToast("取消成功");
    } else {
        isCollect.value = true;
        await collecdAdd({ type: 2, object_id: route.query.articleId });
        showSuccessToast("收藏成功");
    }
    // 重新调用  文章详情
    getDetali();
};

//新增评论
let commentAdd = async () => {
    await addComment({
        type: "2",
        object_id: route.query.articleId,
        content: comment.value,
    });
    // 提示 评论成功

    // 关闭 底部弹出框
    showBottom.value = false;
    //重新获取文章详情  已拿到最新的评论
    getDetali();
};

//获取商品详情 方法
let getDetali = async () => {
    let res = await commentDetail({ id: route.query.articleId });
    info.value = res.data;
    isCollect.value = res.data.is_collect || false;
    isPraise.value = res.data.is_praise || false;
};
// 组件挂载完成
onMounted(async () => {
    getDetali();
});
</script>

<style lang="scss" scoped>
.container {
    background-color: #fff;

    .content {
        .body {
            padding: 0 12px;

            .title {
                margin-top: 10px;
                font-size: 18px;
                font-weight: bold;
                text-align: center;
            }

            .info {
                margin-top: 10px;
                display: flex;
                color: gray;
                justify-content: space-around;
                justify-items: center;
                line-height: 100%;

                .desc {
                    font-size: 14px;
                }

                .created_time {
                    font-size: 14px;
                }
            }

            .detail {
                margin-top: 10px;
                text-indent: 2em;
                font-size: 16px;
                word-wrap: break-word;
                word-break: normal;
                white-space: normal;
            }
        }
    }

    .comment {
        border-top: 1px solid #ccc;
        margin-top: 10px;
        background: #f6f6f6;
        padding: 6px;
        padding-bottom: 50px;

        p {
            font-size: 16px;
        }

        .content_item {
            background: #fff;
            padding: 6px;
            display: flex;
            border-radius: 12px;
            margin-top: 12px;

            .user_pic {
                width: 42px;
                height: 42px;
                position: relative;

                img {
                    width: 42px;
                    height: 42px;
                    object-fit: cover;
                    border-radius: 50%;
                }
            }

            .item_left {
                margin-left: 10px;
                font-size: 14px;
                width: 100%;

                .user_name {
                    margin-bottom: 6px;

                    .createdTime {
                        color: #86909c;
                    }
                }

                .user_content {
                    word-wrap: break-word;
                    word-break: break-all;
                }
            }
        }
    }

    .action_box {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 42px;
        background: #fff;
        border-top: 1px solid rgb(232, 232, 232);
        box-shadow: 0px -2px 10px rgb(232, 232, 232);
        box-sizing: border-box;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .add_comment {
            width: 100%;
            padding-left: 16px;
            height: 100%;
            display: flex;
            justify-content: left;
            align-items: center;
            font-size: 14px;
            color: #767676;

            &:active {
                background: #e8e8e8;
            }
        }

        .action {
            height: 100%;
            width: 100%;
            --van-badge-color: black;
            display: flex;
            // justify-content: space-between;
            align-items: center;
            font-size: 20px;
            color: #767676;

            div {
                display: flex;
                align-items: center;
                justify-content: left;
                padding: 6px 6px 6px 26px;
                height: 100%;
                width: 100%;

                :deep(.van-badge) {
                    color: #767676;
                    background-color: rgba(0, 0, 0, 0);
                    border: none;
                    font-size: 8px;
                    font-weight: normal;
                }

                &:active {
                    background: #e8e8e8;
                    height: 32px;
                }
            }
        }
    }

    .comment_box {
        width: 100%;
        height: 300px;

        .comment_header {
            padding: 10px 16px 0 16px;
            font-size: 16px;
            display: flex;
            justify-content: space-between;

            .left {
                color: v-bind(isHasComment);
            }
        }
    }
}
</style>